import {
  getleavemsgtotal,
  readleaveMessage,
  saveleavemessage,
} from "../model/leavecomment";
import { Express, Request, Response } from "express";
import { LeaveMessage } from "../tool/varType";
// import { dosql } from "../tool/sqlTool";
export default function (app: Express) {
  app.post("/api/addleaveMsg", async function (req: Request, res: Response) {
    let leavemessage: LeaveMessage = req.body;
    const { success, data } = await saveleavemessage(leavemessage);
    if (success) {
      res.json({ success: true, msg: "发布成功" });
    } else {
      res.json({ success: false, msg: "发布失败" });
    }
  });
  app.post(
    "/api/getleavemessage",
    async function (req: Request, res: Response) {
      const { page } = req.body;
      const { success, data } = await readleaveMessage(Number(page));
      console.log(data);

      if (success) {
        const result: any = await getleavemsgtotal();
        console.log(result);

        if (success) {
          res.json({ success: true, data: data, total: result.data[0].total });
          return;
        }
      } else {
        res.json({ success: false, msg: "发布失败" });
      }
    }
  );
}
